package com.example.light.web.meeting.service;

import com.example.light.common.util.SubstringUtil;
import com.example.light.web.meeting.dao.OaRoomMapper;
import com.example.light.web.meeting.entity.OaRoom;
import com.example.light.web.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;

@Service
public class PreorderServiceImpl implements PreorderService {
    @Autowired
    private OaRoomMapper preMapper;

    @Override
    public void preorderList(Model model) {
        List<OaRoom> preorderList = preMapper.querPreorder();
        SubstringUtil.delString(preorderList);
        model.addAttribute("preorderList", preorderList);
    }

    @Override
    public String addPreorder(int preorder, User user) {
        OaRoom room = preMapper.selectByPrimaryKey(preorder);
        if (room.getRoomReserStatus() == 1) {
            return "失败";
        }
        Byte a = 1;
        room.setRoomReserStatus(a);
        room.setRoomReservation(user.getUserName());
        preMapper.updateByPrimaryKeySelective(room);
        return "成功";
    }

    @Override
    public String delPreorder(int delInfo, User user) {
        OaRoom room = preMapper.selectByPrimaryKey(delInfo);
        if (room.getRoomReserStatus() == 1 && user.getUserName().equals(room.getRoomReservation())) {
            Byte a = 0;
            room.setRoomReserStatus(a);
            room.setRoomReservation("");
            preMapper.updateByPrimaryKeySelective(room);
            return "成功";
        }

        if (!user.getUserName().equals(room.getRoomReservation())) {
            return "非对应用户";
        }

        return "失败";

    }
}
